/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2005 INRIA
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
 */
#ifndef IPV4_TEXCP_TABLE_ENTRY_H
#define IPV4_TEXCP_TABLE_ENTRY_H

#include <list>
#include <vector>
#include <ostream>
 
#include "ns3/object.h"
#include "ns3/callback.h"
#include "ns3/traced-value.h" 
#include "ns3/ipv4-address.h"
#include "ns3/traced-value.h"
#include "ns3/packet.h"
#include "texcp-header.h"
#include "ns3/flow-classifier.h"

namespace ns3 {

namespace texcp_routing {


/**
 * \ingroup routing
 *
 * A record of an IPv4 routing table entry for Ipv4GlobalRouting and 
 * Ipv4StaticRouting.  This is not a reference counted object.
 */
class Ipv4TexcpTableEntry : public Object {
public:
  static TypeId GetTypeId (void);

   enum {
    NON_ECT,
    ECT,
    POS,
    CE,
    FNE,
    RECT,
    LE,
    CANC,
    MAX_CODEPOINT
  };
 
  /**
   * \brief This constructor does nothing
   */
  Ipv4TexcpTableEntry ();
  /**
   * \brief Copy Constructor
   * \param route The route to copy
   */
  Ipv4TexcpTableEntry (Ipv4TexcpTableEntry const &route);
  /**
   * \brief Copy Constructor
   * \param route The route to copy
   */
  Ipv4TexcpTableEntry (Ipv4TexcpTableEntry const *route);
  /**
   * \return The IPv4 network number of the destination of this route
   */
  Ipv4Address GetDestNetwork (void) const;
  /**
   * \return The IPv4 network mask of the destination of this route
   */
  Ipv4Mask GetDestNetworkMask (void) const;
  /**
   * \return The Ipv4 interface number used for sending outgoing packets
   */
  uint32_t GetInterface (void) const;
  /**
   * \return the IPv4Address of the egress point associated with this table entry
   */

    /**
   * \brief Updates route reflex counter with sent packet
   * \param codepoint Value of REFLEX codepoint in packet
   * \param value Size of packet
   */
  void UpdateReflex (int codepoint, const uint32_t value);
  /**
    * \brief Updates estimate of loss
    */
  void CalculateLoss ();
  /**
    * \brief Clears packet counters 
    */
  void ClearCount ();
 
  double GetLoss () const;

  struct IEPair GetIEPair (void) const;
  /**
   * \return An Ipv4TexcpTableEntry object corresponding to the input parameters.
   * \param network Ipv4Address of the destination network
   * \param networkMask Ipv4Mask of the destination network mask
   * \param interface Outgoing interface 
   */
  static Ipv4TexcpTableEntry CreateNetworkRouteTo (Ipv4Address network, 
					 Ipv4Mask networkMask, 
					 uint32_t interface,
                                         struct IEPair s);
  /**
    * \return the probe message to be sent
    */
  Ptr<Packet> GetProbe(void);

  
  double GetPathUtilization()
  {
   return m_pathUtilization;
  }
  double GetWeight()
  {
    return m_weight;
  }
  void SetWeight(double w)
  {
   m_weight = w;
  }
  uint64_t GetAllowedPathRate()
  {
    return m_allowedPathRate;
  }
  void SetAllowedPathRate(double w)
  {
   m_allowedPathRate = static_cast<uint64_t>(w);
  }
  int GetPathShaperId()
  {
   return m_pathShaperId;
  }
  void SetPathShaperId(int id)
  {
   m_pathShaperId = id;
  }
  double GetToken()
  {
   return m_tokenCounter;
  }
  void ResetToken()
  {
   m_tokenCounter = 0;
  }

  /*
   * \brief Process the probe to update the path utilization and allowed rate
   */
  void UpdateProbe (TexcpHeader probe);

  double UpdateTokenCounter(uint32_t size);
  /**
   * \brief Signals route was selected, which reduces token counter by the size of the packet 
   */
  void   SelectPath(uint32_t size);

  double GetPathLoss()
  {
   return m_pathLoss;
  }

  private:
  Ipv4TexcpTableEntry (Ipv4Address dest,
	     Ipv4Mask mask,
	     uint32_t interface,
             struct IEPair s);

  Ipv4Address m_dest;
  Ipv4Mask m_destNetworkMask;
  uint32_t m_interface ;
  struct IEPair m_iePair;
  Ptr<Packet> m_probe;

  double m_pathUtilization;
  uint64_t m_allowedPathRate;//g_sp the maximum allowed rate on this path, controlled by core routers using the feedback
  double m_weight;//x_sp the fraction of the pair traffic sent over this path 
  double m_tokenCounter;

  double m_pathLoss;// Loss as indicated by the queues, TEXCP queue must be used

  int m_pathShaperId;

  //reflex counters
  uint32_t m_count[MAX_CODEPOINT];
  uint64_t m_total;

  double m_minLoss;
  double m_alpha;
  TracedValue<double> m_loss;// Loss measured based on LEX protocol
};

std::ostream& operator<< (std::ostream& os, Ipv4TexcpTableEntry const& route);


}}//namespace ns3

#endif /* IPV4_TEXCP_TABLE_ENTRY_H */
